import gulp from 'gulp'
import plugins from 'gulp-load-plugins'
import open from 'open'
const $ = plugins()

const app = {
  srcPath: 'src/',
  budPath: 'build/'
}
gulp.task('lib', () => {
  gulp.src('bower_components/**/*.js')
    .pipe(gulp.dest(app.budPath + 'vendor'))
    .pipe($.connect.reload());
});
gulp.task('html', () => {
  gulp.src(app.srcPath + '**/*.html')
    .pipe(gulp.dest(app.budPath))
    .pipe($.connect.reload());
});
gulp.task('json', () => {
  gulp.src(app.srcPath + 'data/**/*.json')
    .pipe(gulp.dest(app.budPath + 'data'))
    .pipe($.connect.reload());
});
gulp.task('less', () => {
  gulp.src(app.srcPath + 'style/index.less')
    .pipe($.plumber())
    .pipe($.less())
    .pipe($.cssmin())
    .pipe(gulp.dest(app.budPath + 'css'))
    .pipe($.connect.reload());
});
gulp.task('js', () => {
  gulp.src(app.srcPath + 'script/**/*.js')
    .pipe($.concat('index.js'))
    .pipe($.plumber())
    .pipe($.babel())
    .pipe($.uglify())
    .pipe(gulp.dest(app.budPath + 'js'))
    .pipe($.connect.reload());
});
gulp.task('image', () => {
  gulp.src(app.srcPath + 'image/**/*')
    .pipe($.imagemin())
    .pipe(gulp.dest(app.budPath + 'image'))
    .pipe($.connect.reload());
});
gulp.task('clean', function () {
  gulp.src([app.budPath])
    .pipe($.clean());
});
gulp.task('build', ['lib', 'html', 'json', 'less', 'js', 'image']);
gulp.task('serve', ['build'], () => {
  $.connect.server({
    root: [app.budPath],
    livereload: true,
    port: 8080
  });
  open('http://localhost:8080');
  gulp.watch('bower_components/**/*', ['lib']);
  gulp.watch(app.srcPath + '**/*.html', ['html']);
  gulp.watch(app.srcPath + 'data/**/*.json', ['json']);
  gulp.watch(app.srcPath + 'style/**/*.less', ['less']);
  gulp.watch(app.srcPath + 'script/**/*.js', ['js']);
  gulp.watch(app.srcPath + 'image/**/*', ['image']);
});
gulp.task('default', ['serve']);
